# from django.shortcuts import render
from menu.models import SysMenu, SysRoleMenu
from django.http import JsonResponse
from menu.serializer import SysMenuSerializer
from django.views import View
import json
from datetime import datetime


# Create your views here.
class TreeListView(View):
    # 构造菜单树 输入:菜单列表,输出:菜单树 这里不是很理解
    def bulidTreeMenu(self, sysMenuList):
        resultMenuList = []  # 最终的菜单树列表,也是返回给前端的
        menu_dict = {
            menu.id: menu for menu in sysMenuList
        }  # 创建字典快速寻找父节点, key=menu_id, value=menu对象,这里根据order_num排序
        for menu in sysMenuList:
            if menu.parent_id == 0:
                resultMenuList.append(menu)  # 父节点
            else:
                parent = menu_dict.get(menu.parent_id)  # 快速寻找父节点,???
                if not hasattr(parent, "children"):
                    parent.children = []  # 如果父节点没有children属性,则创建一个空列表
                parent.children.append(
                    menu
                )  # 如果父节点有children属性,则将子节点添加到父节点的children属性中
        return resultMenuList

    def get(self, request):
        menuQuerySet = SysMenu.objects.order_by("order_num")  # 按照order_num排序?
        sysMenuList = self.bulidTreeMenu(menuQuerySet)
        serializerMenuList = []  # 序列化菜单列表
        for sysMenu in sysMenuList:
            serializerMenuList.append(SysMenuSerializer(sysMenu).data)
        return JsonResponse(
            {"code": 200, "message": "查询成功", "treeList": serializerMenuList}
        )
class SaveView(View):
    def post(self, request):
        data = json.loads(request.body.decode("utf-8"))
        if data["id"] == -1:  # 新增
            obj_sysMenu = SysMenu(name = data["name"], path = data["path"], component = data["component"], icon = data["icon"], order_num = data["order_num"], parent_id = data["parent_id"], menu_type = data["menu_type"], perms = data["perms"], remark = data["remark"])
            obj_sysMenu.create_time = datetime.now().date()
            obj_sysMenu.save()
        else:  # 修改
            obj_sysMenu = SysMenu(id = data["id"], name = data["name"], path = data["path"], component = data["component"], icon = data["icon"], order_num = data["order_num"], parent_id = data["parent_id"], menu_type = data["menu_type"], perms = data["perms"], remark = data["remark"], create_time = data["create_time"], update_time = data["update_time"])
            obj_sysMenu.update_time = datetime.now().date()
            obj_sysMenu.save()
        return JsonResponse({"code": 200, "message": "保存成功"})

class ActionView(View):
    def get(self, request):
        id = request.GET.get("id")
        menu_objects = SysMenu.objects.get(id=id)
        return JsonResponse({"code": 200, "message": "查询成功", "menu": SysMenuSerializer(menu_objects).data})
    
    def delete(self, rerqust):
        id = json.loads(rerqust.body.decode("utf-8"))
        if SysMenu.objects.filter(parent_id = id).count() > 0:
            return JsonResponse({"code": 500, "message": "该菜单存在子菜单,不能删除"})
        else:
            SysRoleMenu.objects.filter(menu_id = id).delete()  # 删除角色菜单关联表中的数据
            SysMenu.objects.filter(id = id).delete()  # 删除菜单
            return JsonResponse({"code": 200, "message": "删除成功"})
        
        
            